package PangModel;

import java.awt.*;
import java.awt.image.ImagingOpException;

/**
 * Created by YILMAZ on 23/4/2015.
 */
/*
 * Hero tick method should be called every GamePace ms
 */

public class Hero extends GameObject {
    private int lives = GAME_NUMBER_OF_LIVES;


    public Hero() {
        height = HERO_HEIGHT;
        width = HERO_WIDTH;
        imageID = ImageStore.IMAGE_HERO_STABLE;
        direction = new Point(0,0);
    }

    boolean canShoot = true;
    private boolean isImmune = false;

    double immuneCountDown = 0.0;
    
    public boolean canShoot() {
        return canShoot;
    }

    /*void setDirection(int direction)
    {
        Point dir = new Point();
        if (direction == GameObject.LEFT)
        {
            dir.setLocation(UNIT_SPEED*GameObject.HERO_SPEED, 0);
            setDirection(dir);
        }
        else if (direction == GameObject.RIGHT)
        {
            dir.setLocation(0, UNIT_SPEED*GameObject.HERO_SPEED);
            setDirection(dir);
        }
        //else throw Exception;
    }*/

    public void move()
    {

    }

    public void move(int direction) throws UnsupportedArgumentException {

        if(direction == GameObject.LEFT)
        {
            double newx = coordinates.getX() - GameObject.UNIT_SPEED * GameObject.HERO_SPEED;
            coordinates.setLocation(newx, coordinates.getY());
          //  imageID = ImageStore.IMAGE_HERO_LEFT;
        }
        else if(direction == GameObject.RIGHT)
        {
            double newx = coordinates.getX() + GameObject.UNIT_SPEED * GameObject.HERO_SPEED;
            coordinates.setLocation(newx, coordinates.getY());
           // imageID = ImageStore.IMAGE_HERO_RIGHT;
        }

        else throw new UnsupportedArgumentException("Unsupported argument for Hero move method: " + direction);
    }

    /*public void activateImmuneCountDown() {

    }*/

    public boolean isImmune() {
        return isImmune;
    }

    public void addItem(Item item) throws UnsupportedArgumentException // also change imageID when needed
    {
        int itemType = item.getItemType();

        switch (itemType)
        {
            case Item.POWERUP_IMMUNITY: makeImmune();
                break;
            default: throw new UnsupportedArgumentException("Unsupported argument for Hero addItem method: " + item);
        }
    }

    private void  makeImmune()
    {
        isImmune = true;
        immuneCountDown = GameObject.GAME_IMMUNITY_TIME;
    }

    private void tick()
    {
        imageID = ImageStore.IMAGE_HERO_STABLE; // NOT SURE IF THIS WILL WORK

        if (isImmune == false)
            return;

        if (GameMap.GAME_PACE == 0)
            return; // throw exception

        immuneCountDown = immuneCountDown - GameMap.GAME_PACE;

        if (immuneCountDown < 0)
        {
            immuneCountDown = 0;
            isImmune = false;
            //imageID = 1;
        }
    }

    private void removePowerUp(Item item) // do we realy need this?
    {

    }

    public boolean hit()
    {
        if(isImmune)
        {
            isImmune = false;
            imageID = ImageStore.IMAGE_HERO_STABLE;
            return false;
        }
        else
        {
           return decrementLives();
        }
    }

    private boolean decrementLives()
    {
        --lives;
        System.out.println("lives is : "+ lives) ;

        return lives== 0;
    }
}
